#include "../Header Files/C3DNode_AL.h"
#include "../Header Files/CAssetManager_AL.h"
#include "../Header Files/Direct3D_AL.h"

C3DNode_AL::C3DNode_AL(void)
{
	m_pTexture = NULL;
	D3DXMatrixIdentity(&m_mPosition);
	m_sRenderType = 2;
}
C3DNode_AL::C3DNode_AL(IDirect3DTexture9* _pTexture, D3DXMATRIX _mPos, short _sRenderType)
{
	m_pTexture = _pTexture;
	m_mPosition = _mPos;
	m_sRenderType = _sRenderType;
	m_dColor = D3DCOLOR_XRGB(255, 255, 255);
}
C3DNode_AL::~C3DNode_AL(void)
{
	m_pTexture->Release();
}
void C3DNode_AL::Draw(cCamera_AL* _pCamera)
{
	switch (m_sRenderType)
	{
	case 2:
		{
			ID3DXEffect* effect = CAssetManager_AL::GetInstance()->GetEffect(Shaders::Standard_Defuse);

			if (effect)
			{
				effect->SetTechnique("Basic");
				unsigned int passes(0);
				effect->Begin(&passes, 0);

				for (unsigned int i(0); i < passes; ++i)
				{
					effect->BeginPass(i);
					{
						effect->SetMatrix("gWVP", &(m_mPosition * _pCamera->GetViewMatrix(false) * _pCamera->GetProjectionMatrix()));
	/*HARDCODED*/		effect->SetFloat("fScale", 1.0f);
						D3DCOLOR col = D3DCOLOR(0xff0000ff);
						effect->SetValue("Color", &col, sizeof(float) * 4);
						effect->CommitChanges();

						Direct3D_AL::GetInstance()->m_pD3DDev->SetVertexDeclaration(m_vVertDecl);
						Direct3D_AL::GetInstance()->m_pD3DDev->SetStreamSource(0, m_vVertBuffer, 0, sizeof(Verts_PosCol));
						Direct3D_AL::GetInstance()->m_pD3DDev->SetIndices(m_vIndexBuffer);
						Direct3D_AL::GetInstance()->m_pD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 36, 0, 12);
					} 
					effect->EndPass();
				}
				effect->End();
			}

		}
		break;
	case 3:
		{

			ID3DXEffect* effect = CAssetManager_AL::GetInstance()->GetEffect(Shaders::Ground_Effect);

			if (effect)
			{
				effect->SetTechnique("Basic");
				unsigned passes(0);
				effect->Begin(&passes, 0);
				for (unsigned i(0); i<passes; ++i)
				{
					effect->BeginPass(i);
					{
						effect->SetMatrix("gWVP", &(m_mPosition * _pCamera->GetViewMatrix(true) * _pCamera->GetProjectionMatrix()));
						effect->SetTexture("groundTex", CAssetManager_AL::GetInstance()->GetTexture(1));
						effect->CommitChanges();

						Direct3D_AL::GetInstance()->m_pD3DDev->SetStreamSource(0, CAssetManager_AL::GetInstance()->GetVertBuff(1), 0, sizeof(Verts_PosUV));
						Direct3D_AL::GetInstance()->m_pD3DDev->SetVertexDeclaration(CAssetManager_AL::GetInstance()->GetVertDecl(1));
						Direct3D_AL::GetInstance()->m_pD3DDev->SetIndices(CAssetManager_AL::GetInstance()->GetIndextBuff(1));
						Direct3D_AL::GetInstance()->m_pD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 2);
					}
					effect->EndPass();
				}
				effect->End();
			}
		}
		break;
	}
}